#include "Tank.h"
#include <QImage>
#include <QPoint>
#include "Gamemap.h"
#include <QDebug>

extern GameMap* gameMap;
Tank::Tank(int key1,int key2,int key3,int key4,int shoot,QObject *parent) : QObject(parent),
    my_up(key1),my_down(key2),my_left(key3),my_right(key4),my_shoot(shoot)
{
    tank=QImage(":/map_image/element/tank.png");
    pos_x=tank.width();
    pos_y=tank.height();
    moveStep=20 ;
    QuickmoveStep=10;
    mydir=xup;
    HP=30;
    this->isCreated=false;
    shootTimer=new QTimer(this);
    shootTimer->start(100);
    for(int i=0;i<5;i++)
    {
        mybullet[i].mTank=(void*)this;
        connect(shootTimer,&QTimer::timeout,&mybullet[i],&Bullet::BulletMove);
    }
}

void Tank::DrawTank(QPainter* p,direction d)
{
    qDebug()<<"要开始画坦克了"<<this->HP;
    if(this->HP > 0)
    {
        p->save();
        p->translate(QPoint(pos_x,pos_y));
        p->rotate(qreal(90)*d);
        if(d == xup)
        {
            p->drawImage(0,0,tank);
        }
        else if(d == xright)
        {
            p->drawImage(0,-tank.height(),tank);
        }
        else if(d == xdown)
        {
            p->drawImage(-tank.width(),-tank.height(),tank);
        }
        else if(d == xleft)
        {
            p->drawImage(-tank.width(),0,tank);
        }
        p->restore();
    }
}

void Tank::move(int dx,int dy)
{
    pos_x=dx+pos_x;
    pos_y=dy+pos_y;
}

bool Tank::iscollide(int dx, int dy)
{
    QImage mywall(":/map_image/element/wall.gif");
    QImage mytank(":/map_image/element/tank.png");
    int newx=this->pos_x+dx;
    int newy=this->pos_y+dy;
    if(newx>=(gameMap->col-1)*mywall.width() || newy>=(gameMap->row-1)*mywall.height() || newx<=0 || newy <=0)
    {
        qDebug()<<"坦克试图越界";
        return true;
    }
    int countPoint=0,px=0,py=0;
    for(int i=0;i<gameMap->col;i++)
    {
        for(int j=0;j<gameMap->row;j++)
        {
            int  inx=i*mywall.width();
            int  iny=j*mywall.height();
            if((inx >= newx && inx <= newx+mytank.width())&&(iny>=newy && iny <=newy+mytank.height()))
            {
                px=j;
                py=i;
                countPoint++;
            }
        }
    }
    switch (countPoint)
    {
    case 1:
    {
        if(gameMap->Gmap[px-1][py-1] == wall ||gameMap->Gmap[px][py-1] == wall ||gameMap->Gmap[px-1][py] == wall ||gameMap->Gmap[px][py] == wall)
            return true;
        else return false;
    }
    case 2:
    {
        if((px-1)*mywall.height()>=newy && (px-1)*mywall.height()<=newy+mytank.height())
        {
            if(gameMap->Gmap[px-1][py-1] == wall || gameMap->Gmap[px-1][py] == wall) return true;
            else return false;
        }
        else
        {
            if(gameMap->Gmap[px-1][py-1] == wall || gameMap->Gmap[px][py-1] == wall) return true;
            else return false;
        }
    }
    case 4:
    {
        if(gameMap->Gmap[px-1][py-1] == wall) return true;
        else return false;
    }
    default:
    {
        qDebug()<<"相撞判定错误！ 请重试";
        return true;
    }
    }
}

void Tank::HowMove(QKeyEvent *event)
{
    if(event->isAutoRepeat())
    {
        if(event->key() == my_up)
        {
            this->mydir=xup;
            int i;
            for(i=0;!this->iscollide(0,-i)&& i<=this->QuickmoveStep;i++);
            i--;
            this->move(0,-i);
        }
        else if(event->key() == my_down)
        {
            this->mydir=xdown;
            int i;
            for(i=0;!this->iscollide(0,i)&& i<=this->QuickmoveStep;i++);
            i--;
            this->move(0,i);
        }
        else if(event->key() == my_left)
        {
            this->mydir=xleft;
            int i;
            for(i=0;!this->iscollide(-i,0)&& i<=this->QuickmoveStep;i++);
            i--;
            this->move(-i,0);
        }
        else if(event->key() == my_right)
        {
            this->mydir=xright;
            int i;
            for(i=0;!this->iscollide(i,0)&& i<=this->QuickmoveStep;i++);
            i--;
            this->move(i,0);
        }
        return;
    }
    if(event->key() == my_up)
    {
        this->mydir=xup;
        int i;
        for(i=0;!this->iscollide(0,-i)&& i<=this->QuickmoveStep;i++);
        i--;
        this->move(0,-i);
    }
    else if(event->key() == my_down)
    {
        this->mydir=xdown;
        int i;
        for(i=0;!this->iscollide(0,i)&& i<=this->QuickmoveStep;i++);
        i--;
        this->move(0,i);
    }
    else if(event->key() == my_left)
    {
        this->mydir=xleft;
        int i;
        for(i=0;!this->iscollide(-i,0)&& i<=this->QuickmoveStep;i++);
        i--;
        this->move(-i,0);
    }
    else if(event->key() == my_right)
    {
        this->mydir=xright;
        int i;
        for(i=0;!this->iscollide(i,0)&& i<=this->QuickmoveStep;i++);
        i--;
        this->move(i,0);
    }
}

void Tank::HowShoot(QKeyEvent *event)
{
    if(event->isAutoRepeat()) return;
    else if(event->key() == my_shoot)
    {
        for(int i=0;i<5;i++)
        {
            if(mybullet[i].isshoot == false)
            {
                mybullet[i].my_dir=this->mydir;
                mybullet[i].pos_x=this->pos_x+24;
                mybullet[i].pos_y=this->pos_y+24;
                mybullet[i].isshoot = true;
                break;
            }
        }
    }
}

